import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:syn_4s/core/extension/build_context_extension.dart';
import 'package:syn_4s/core/extension/int_extension.dart';
import 'package:syn_4s/core/model/safety_training_model.dart';
import 'package:syn_4s/core/utils/date_format_utils.dart';
import 'package:syn_4s/ui/shared/app_theme.dart';
import 'package:syn_4s/ui/widgets/safety_training_item.dart';

import '../../core/viewmodel/main_view_model.dart';
import 'custom_ui.dart';
import 'dashed_line.dart';
import 'main_fold.dart';

class MainSafetyTraining extends StatefulWidget {
  final Key? scrollKey;
  final MainViewModel viewModel;
  final String? title;

  const MainSafetyTraining(
      {super.key, required this.viewModel, this.scrollKey,this.title});

  @override
  State<MainSafetyTraining> createState() => _MainSafetyTrainingState();
}

class _MainSafetyTrainingState extends State<MainSafetyTraining> {
  final ScrollController _controller = ScrollController();

  @override
  void initState() {
    super.initState();
    addScrollListener(_controller, widget.viewModel.isSafetyScrollable);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MainFold(
      scrollKey: widget.scrollKey,
      title: widget.title??context.l10n.safety_training,
      child: Column(
        children: [
          SafetyTrainingItem(
            padding: EdgeInsets.only(left: 6.px, right: 6.px, bottom: 2.px),
            name: context.l10n.name,
            time: context.l10n.time,
            curriculum: context.l10n.curriculum,
            fraction: context.l10n.fraction,
          ),
          SizedBox(
            height: 240.px,
            child: Obx(() => ListView.separated(
                controller: _controller,
                physics: widget.viewModel.isSafetyScrollable.value
                    ? const AlwaysScrollableScrollPhysics()
                    : const NeverScrollableScrollPhysics(),
                // shrinkWrap: true,
                padding: EdgeInsets.only(left: 6.px, right: 6.px),
                itemBuilder: (ctx, index) {
                  Vr vr = widget.viewModel.vrs[index];
                  return SafetyTrainingItem(
                    padding: EdgeInsets.only(top: 4.px, bottom: 4.px),
                    name: vr.staffName ?? "",
                    time: DateFormatUtils.dateTimeToStr(vr.startTT,
                        format: "dd-MM-yyyy HH:mm:ss"),
                    curriculum: vr.curriculum ?? "",
                    fraction: vr.fractlonName,
                    fractionColor: (vr.fractlon ?? 1) == 0
                        ? AppTheme.redColor
                        : AppTheme.orangeColor,
                  );
                },
                separatorBuilder: (ctx, index) {
                  return const DashedLine(
                    color: Colors.white,
                  );
                },
                itemCount: widget.viewModel.vrs.length)),
          ),
        ],
      ),
    );
  }

  Widget buildItem(BuildContext context, {bool offline = false}) {
    Color? dataColor = offline ? AppTheme.redColor : null;
    return Container(
      padding: EdgeInsets.only(left: 5.px),
      decoration:
          BoxDecoration(border: Border.all(color: Colors.white, width: 1.px)),
      child: IntrinsicHeight(
        child: Row(
          children: [
            VerticalDivider(
              color: offline ? AppTheme.redColor : AppTheme.greenColor,
              width: 6.px,
              thickness: 6.px,
              indent: 4.px,
              endIndent: 4.px,
            ),
            buildInfo(context),
            Container(
              width: 1.px,
              color: Colors.white,
            ),
            buildTopBottom(context, context.l10n.heart_rate, color: dataColor),
            Container(
              width: 1.px,
              color: Colors.white,
              margin: EdgeInsets.symmetric(vertical: 16.px),
            ),
            buildTopBottom(context, context.l10n.body_temp, color: dataColor),
          ],
        ),
      ),
    );
  }

  Widget buildInfo(BuildContext context) {
    return Expanded(
        flex: 2,
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 2.px, horizontal: 6.px),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "王一",
                style: TextStyle(fontSize: 18.px),
              ),
              buildLongText("03-07-2024 12:00:00"),
              buildLongText(
                "Address:123 Po Kong Village Rdsdfsdbgfb",
              ),
            ],
          ),
        ));
  }

  Widget buildLongText(String data) {
    return Text(
      data,
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );
  }

  Widget buildTopBottom(BuildContext context, String title, {Color? color}) {
    return Expanded(
        child: Container(
      margin: EdgeInsets.symmetric(vertical: 2.px, horizontal: 4.px),
      child: Column(
        children: [
          Text(
            "76",
            style: TextStyle(
                color: color ?? AppTheme.textColor,
                fontWeight: FontWeight.w900,
                fontSize: 20.px),
          ),
          Text(
            textAlign: TextAlign.center,
            title,
          ),
        ],
      ),
    ));
  }
}
